var url1;
var col = [];
var componen = 'setup_'+type;

col[0] = new Array();

$(".datebox").datebox({
	formatter: formatDate,
	'setValue':'today'
});

/*$('#tgl_pemesanan_awal').datebox({formatter: formatDate });
		$('#tgl_pemesanan_akhir').datebox({formatter: formatDate });
		$('#tgl_pemesanan_awal').datebox('setValue',today);
		$('#tgl_pemesanan_akhir').datebox('setValue',today);*/

if(type == "user_list"){
	
	url1 = host+"setup/getData/user_list";
        col[0][0] =   {field:'id',title:'ID',width:200,halign:'center',hidden:true};
	col[0][1] =   {field:'username',title:'Username',width:200,halign:'center'};
        col[0][2] =   {field:'fullname',title:'Full Name',width:200,halign:'center'};
        col[0][3] =   {field:'email',title:'Email',width:200,halign:'center'};
        col[0][4] =   {field:'position',title:'Position',width:200,halign:'center'};
        col[0][5] =   {field:'level',title:'Level',width:200,halign:'center'};
        col[0][6] =   {field:'group',title:'Group',width:200,halign:'center'};
        col[0][7] =   {field:'sub_group',title:'Sub Group',width:200,halign:'center'};
        //col[0][5] =   {field:'',title:'Kode Pos',width:100,halign:'center', align:'right'};
	
	
}else if(type == "user_level"){	
	url1 = host+"setup/getData/user_level";
	col[0][0] = {field:'id',title:'ID',hidden:true};
        col[0][1] = {field:'level',title:'Level',width:300,hidden:false};	
}else if(type == "user_group"){
	url1 = host+"setup/getData/user_group";
	col[0][0] = {field:'id',title:'ID', hidden:true};
        col[0][1] = {field:'group',title:'Group',width:300, hidden:false};
}else if(type == "user_role"){
	url1 = host+"setup/getData/user_role";	
	col[0][0] ={field:'role',title:'Role',width:300,hidden:false};
        col[0][1] = {field:'id',title:'Policy',width:200,formatter:function(index,value){
                        return "<center><a href='#' onClick='role("+index+");'>SET</a></center>";
                    }};
       
}else if(type == "app_modules"){
	url1 = host+"setup/getData/app_modules";	
	col[0][0] = {field:'id',title:'ID', hidden:true};
        col[0][1] = {field:'module_name',title:'Module',width:300, hidden:false};				
	
			
}else if(type == "app_functions"){	
	url1 = host+"setup/getData/app_functions";
	col[0][0] = {field:'id',title:'ID', hidden:true};
        col[0][1] = {field:'tbl_module_id',title:'Module ID',width:300, hidden:true};
        col[0][2] = {field:'module_name',title:'Module',width:300, hidden:false};
        col[0][3] = {field:'function_name',title:'Function',width:300, hidden:false};
}

$("#"+type).datagrid({
    title:'',
    height:frmHeight-250,
    width:frmWidth-280,
    iconCls: 'table',
    rownumbers:true,
    fit:false,
    striped:false,
    pagination:true,
    sortable:true,
    url:url1,		
    nowrap: true,
    singleSelect:true,
    pagination:true,
    onLoadSuccess: function(data){
        if(data.total == 0){
            var $panel = $(this).datagrid('getPanel');
            var $info = '<div class="info-empty">Tidak Ada Data</div>';
            $($panel).find(".datagrid-view2 .datagrid-body").append($info);
                            //$('#edit_data').linkbutton({disabled:true});
                            //$('#delete_data').linkbutton({disabled:true});
        }
    },
    toolbar:'#toolbar_'+type,		
    columns:col,
});
	
function add(){
	tinggiregion = frmHeight-248;
	$('#'+componen).html('');
	$('#'+componen).addClass("loading");
	$.post('setup/getform/add/'+type,{'tinggiregion':tinggiregion},function(resp){
		//windowForm(resp, judulwindowAdd, lebarjendelaForm, tinggijendelaForm);
		$('#'+componen).removeClass('loading')
		$('#'+componen).html(resp);
	});
}
function role(id){
	tinggiregion = frmHeight-248;
	$('#'+componen).html('');
	$('#'+componen).addClass("loading");
	$.post('setup/policy/'+id,{'tinggiregion':tinggiregion,'id':id},function(resp){
		//windowForm(resp, judulwindowAdd, lebarjendelaForm, tinggijendelaForm);
		$('#'+componen).removeClass('loading')
		$('#'+componen).html(resp);
	});
}
function edit(){
	tinggiregion = frmHeight-248;
	var datamaster = $("#"+type).datagrid('getSelected');
	
	if(datamaster){
		$('#'+componen).html('');
		$('#'+componen).addClass("loading");
		$.post('setup/getform/edit/'+type,{'tinggiregion':tinggiregion, 'id':datamaster.id},function(resp){
			//windowForm(resp, judulwindowAdd, lebarjendelaForm, tinggijendelaForm);
			$('#'+componen).removeClass('loading')
			$('#'+componen).html(resp);
		});
	}else{
		$.messager.alert('warning','Pilih Data Yang Akan Diedit!!','warning');
	}
}

function del() {
	var datamaster = $("#"+type).datagrid('getSelected');
	var dataval = { 'id':datamaster.id };
	
	if (type == 'sparepart' || type == 'generator' || 
		type == 'peralatan_service' || type == 'barang_promosi') {
			dataval = { 'id':datamaster.id, 'kdgambar':datamaster.kode_gambar };
		}
	
	if(datamaster){
		$.messager.confirm('Confirm','Anda yakin ingin menghapus data ini?', function(res) {
			if (res) {
				$('#'+componen).addClass("loading");
				$.post(host+'setup/deleteData/'+type, dataval, function(resp){
					$('#'+componen).removeClass('loading');
					$("#"+type).datagrid('reload');
					if(resp == '0') {
						$.messager.alert('warning','Data gagal terhapus!!','warning');
					}
				});
			}
		});
	}else{
		$.messager.alert('warning','Pilih Data Yang Akan Dihapus!!','warning');
	}
}

function search(type="") {

}